package org.grottarossa.ui.core.casemanagement.bpm.actionsbar;

import java.util.ResourceBundle;

import org.grottarossa.template.data.pojo.workflows.WorkItem;
import org.grottarossa.template.data.pojo.workflows.Workflow;
import org.grottarossa.ui.core.ApplicationSession;
import org.grottarossa.ui.core.base.IBaseComponent;
import org.grottarossa.ui.core.controls.searchuser.ISearchUserListener;
import org.grottarossa.ui.core.controls.searchuser.SearchUser;
import org.grottarossa.ui.core.controls.searchuser.SearchUserEvent;
import org.grottarossa.ui.core.controls.searchuser.SearchUserFactory;

import com.vaadin.ui.Window;



/***
 * Gestisce la logica della buttons bar di gestione lock/unlock/suspend/assign task
 * @author 
 *
 */
public class LockBarPresenter implements ISearchUserListener
{
	private final ResourceBundle m_localizer;

	/*** View ***/
	private LockBar m_view;

	/*** Stato del pulsante lock/unlock  ***/
	private boolean m_locked;

	/*** Workflow di riferimento ***/
	private final Workflow m_workflow;

	/*** WorkItem di riferimento ***/
	private final WorkItem m_workInstance;

	private Window m_assignPopup;
	private final IBaseComponent m_viewer;

	private final ApplicationSession m_appSession;

	public LockBarPresenter(ApplicationSession appSession, LockBar view, Workflow workflow, WorkItem instance, ResourceBundle localizer, IBaseComponent viewer)
	{
		this.m_appSession = appSession;
		m_view = view;
		m_workflow = workflow;
		m_workInstance = instance;
		m_localizer = localizer;
		m_viewer = viewer;
		m_view.setPresenter(this);
	}

	public void initialize()
	{
		m_locked = m_workInstance.isLocked();

		m_view.setSuspendVisible(m_locked);

		m_view.setChangeLockStatusButtonIcon(m_locked);
	}

	/***
	 * Esegue il suspend del WorkItem di riferimento
	 */
	public void suspend()
	{
		try
		{
			m_appSession.getBuilder().getWorkflowManagement().suspendWorkItem(m_workflow, m_workInstance);
		}
		catch(Exception ex)
		{
			m_view.showError(ex);
		}
	}

	/***
	 * Esegue il lock o l'unlock del WorkItem di riferimento
	 */
	public void changeLockStatus()
	{
		try
		{
			if(!m_locked)
				m_appSession.getBuilder().getWorkflowManagement().lockWorkItem(m_workflow, m_workInstance);
			else
				m_appSession.getBuilder().getWorkflowManagement().unlockWorkItem(m_workflow, m_workInstance);

			m_locked = !m_locked;
			m_view.setSuspendVisible(m_locked);

			m_view.setChangeLockStatusButtonIcon(m_locked);
		}
		catch(Exception ex)
		{
			m_view.showError(ex);
		}
	}

	/***
	 * Mostra il popup per l'assegnamento dell'utente/ruolo al WorkItem di riferimento
	 */
	public void assign()
	{
		SearchUserFactory f = new SearchUserFactory(m_appSession);
		SearchUser su = f.createUserSearcher();
		su.addListener(this);
		m_assignPopup = m_view.showInPopup(su, 500, 400, "", true, true, m_view.getAssignButtonWindow());
	}

	/***
	 * Gestisce la selezione dell'utente da assegnare al WorkItem di riferimento
	 */
	public void ok(SearchUserEvent event)
	{
		try
		{
			m_appSession.getBuilder().getWorkflowManagement().assignWorkItem(m_workflow, m_workInstance, event.getSelected());
		}
		catch(Exception ex)
		{
			m_viewer.showError(ex);
		}

		m_view.removePopupFromWindow(m_assignPopup, m_view.getAssignButtonWindow());
	}

	/***
	 * Gestisce l'annullamento della selezione dell'utente da assegnare al WorkItem di riferimento
	 */
	public void cancel()
	{
		m_view.removePopupFromWindow(m_assignPopup, m_view.getAssignButtonWindow());
	}

}
